﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpObjects.SearchEngine.HtmlParsing.Html;

namespace SharpObjects.SearchEngine.HtmlParsing.Parser
{
    public class HtmlParserDocument : HtmlContainerTag
    {
        public override string Name { get; set; }

        private HtmlTagFactory tagFactory;

        public HtmlParserDocument(HtmlTagFactory tagFactory)
        {
            this.tagFactory = tagFactory;
            this.currentTag = new Stack<HtmlTag>();
            this.currentTag.Push(this);
        }

        private Stack<HtmlTag> currentTag;
        
        public override bool IsContent { get { return true; } set { } }

        public TextFragment Title { get; set; }
        public List<TextFragment> TextForIndexing = new List<TextFragment>();

        public void Add(HtmlGenericTag htmlGenericTag)
        {
            HtmlTag newTag = this.tagFactory.CreateTag(htmlGenericTag);

            if(newTag.IsClosed)
            {
                if (newTag.IsContent && this.currentTag.Peek().IsContent)
                {
                    if (newTag.Text != null && newTag.Text.Trim() != string.Empty)
                    {
                        TextFragment text = new TextFragment(){Tag = newTag.Name, IndexingBoost = newTag.IndexingBoost, Text = newTag.Text};

                        if (newTag.Name == HtmlText.TagName)
                        {
                            text.IndexingBoost = this.currentTag.Peek().IndexingBoost;
                            text.Tag = this.currentTag.Peek().Name;
                        }

                        if (this.currentTag.Peek().Name == HtmlTitleTag.TagName)
                        {
                            this.Title = text;
                        }
                        else
                        {
                            this.TextForIndexing.Add(text);
                        }
                    }
                }
            }
            else
            {
                if(newTag.IsClosing)
                {
                    if(this.currentTag.Peek().Name == newTag.Name)
                    {
                        this.currentTag.Pop();
                    }
                }
                else
                {
                    if (!newTag.IsInline)
                    {
                        if (this.currentTag.Peek().Name != HtmlScriptTag.TagName)
                        {
                            this.currentTag.Push(newTag);

                            // whether this tag has indexable text in attributes

                            if (newTag.HasText && newTag.Text != null && newTag.Text.Trim() != string.Empty)
                            {
                                TextFragment text = new TextFragment() { Tag = newTag.Name, IndexingBoost = newTag.IndexingBoost, Text = newTag.Text };
                                this.TextForIndexing.Add(text);
                            }
                        }
                    }
                }
            }

        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();

            if (this.Title != null)
            {
                sb.Append("Title ");
                sb.Append(this.Title.ToString());
                sb.Append("\n");
            }

            foreach(TextFragment text in this.TextForIndexing)
            {
                sb.Append(text.ToString());
                sb.Append("\n");
            }


            return sb.ToString();
        }

    }
}
